package com.lfec.dividiraconta;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class NovaPessoaActivity extends ActionBarActivity {

	private EditText ediText;
	private ListView listView;
	private ArrayAdapter<Pessoa> arrayAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nova_pessoa);

		ediText = (EditText) findViewById(R.id.novaPessoaEditText);
		listView = (ListView) findViewById(R.id.novaPessoaList);

		arrayAdapter = new PessoaAdapter(this);

		listView.setAdapter(arrayAdapter);

		cleanAndReinstall();
	}

	private void cleanAndReinstall() {
		arrayAdapter.clear();

		for (Pessoa p : DadosHolder.getInstance().getPessoaList()) {
			arrayAdapter.add(p);
		}
	}

	public void adicionarPessoa(View view) {
		String nomePessoa = ediText.getText().toString();

		if (!nomePessoa.equals("")) {

			Pessoa p = new Pessoa();
			p.setNome(nomePessoa);

			DadosHolder.getInstance().addPessoa(p);

			arrayAdapter.add(p);
			ediText.setText("");
		} else {
			Toast.makeText(this, R.string.insiraNome, Toast.LENGTH_SHORT).show();
		}
	}

	public void voltar(View view) {
		finish();
	}

}
